//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2009 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// GotoDayDialog.cpp : implementation file
//

#include "stdafx.h"
#include "GotoDayDialog.h"
#include "../dll_object.h"
#include "../multi_lang_cal_impl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define  GET_RES_STRING(strid) dll_object::multi_lang_impl_.get_res_string(strid)
/////////////////////////////////////////////////////////////////////////////
// CGotoDayDialog dialog

CGotoDayDialog::CGotoDayDialog(const COleDateTime& date, 
							   CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE type,
							   int nFirstDayOfWeek /*= 0*/, 
							   CWnd* pParent /*=NULL*/)
							   : CDialogBase(CGotoDayDialog::IDD, pParent)
							   , m_Date (date)
							   , m_Type (type)
							   , m_nFirstDayOfWeek(0)
{
	//{{AFX_DATA_INIT(CGotoDayDialog)
	m_TypeIndex = -1;
	//}}AFX_DATA_INIT

	ASSERT (0 <= nFirstDayOfWeek);
	ASSERT (nFirstDayOfWeek <= 6);

	m_nFirstDayOfWeek = nFirstDayOfWeek;

	switch (m_Type)
	{
	case CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_DAY:
		m_TypeIndex = 0;
		break;
	case CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_WORK_WEEK:
		m_TypeIndex = 1;
		break;
	case CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_WEEK:
		m_TypeIndex = 2;
		break;
	case CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_MONTH:
		m_TypeIndex = 3;
		break;
	}
}

void CGotoDayDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogBase::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGotoDayDialog)
	DDX_Control(pDX, IDC_DATETIMEPICKER, m_DateTimePicker);
	DDX_CBIndex(pDX, IDC_MODE_COMBO, m_TypeIndex);
	DDX_Control(pDX, IDC_MODE_COMBO, m_wndCombo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGotoDayDialog, CDialogBase)
	//{{AFX_MSG_MAP(CGotoDayDialog)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CGotoDayDialog::OnOK ()
{
	if (!UpdateData ())
	{
		return;
	}

	CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE nType [] =
	{
		CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_DAY,
		CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_WORK_WEEK,
		CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_WEEK,
		CBCGPPlannerManagerCtrl::BCGP_PLANNER_TYPE_MONTH,
	};

	m_Type = nType [m_TypeIndex];
	m_DateTimePicker.GetTime(m_Date);
	
	g_dll_obj->erase_lock_wnd(this);

	CDialogBase::OnOK ();
}

/////////////////////////////////////////////////////////////////////////////
// CGotoDayDialog message handlers

BOOL CGotoDayDialog::OnInitDialog() 
{
	CDialogBase::OnInitDialog();
	SetDpi(IDD);

	HICON icon=AfxGetApp()->LoadIcon (IDR_MAINFRAME);
	SetIcon(icon, TRUE);
	SetIcon(icon, FALSE);

	DateTime_SetFormat(m_DateTimePicker.GetSafeHwnd(),"''yyy'-'MM'-'dd' 'dddd''");
	m_DateTimePicker.SetTime(m_Date);
	
	if (!m_bMulti)
	{
		m_wndCombo.DeleteString (m_wndCombo.GetCount () - 1);
	}
	reset_dlg_size();
	ChangeLanguage();
	DSkinLiteSkin();
	add_anchor_ex();
	
	set_window_pos();

	dsSkinWindow(GetDlgItem(IDOK)->GetSafeHwnd(),SKIN_TYPE_BUTTON, "button", FALSE);
	dsSkinWindow(GetDlgItem(IDCANCEL)->GetSafeHwnd(),SKIN_TYPE_BUTTON, "button_default", FALSE);

	g_dll_obj->push_lock_wnd(this);

	return TRUE; 
}

void 
CGotoDayDialog::reset_dlg_size()
{
	int xscreen = ::GetSystemMetrics(SM_CXSCREEN);
	int yscreen = ::GetSystemMetrics(SM_CYSCREEN);
	CRect cr;
	cr.left = (xscreen - e_calendar_goto_day_width_ )/2;
	cr.top = (yscreen - e_calendar_goto_day_height_ )/2;
	cr.right = cr.left + e_calendar_goto_day_width_;
	cr.bottom = cr.top + e_calendar_goto_day_height_;

	if( ::IsWindow(this->m_hWnd) )
	{
		this->MoveWindow(cr);
	}
}

void 
CGotoDayDialog::set_window_pos()
{
	CRect r;
	GetClientRect(r);
	if ( 1 )
	{
		r.top = r.bottom - 28;
		r.bottom = r.top + 21;

		r.left = r.right - 142;
		r.right = r.left + 54;

		GetDlgItem(IDOK)->MoveWindow(r, FALSE);	

		r.left = r.right + 8;
		r.right = r.left + 54;
		GetDlgItem(IDCANCEL)->MoveWindow(r, FALSE);	
	}
}
void 
CGotoDayDialog::ChangeLanguage()
{
	this->SetWindowText(GET_RES_STRING(IDS_GOTODAY_DLG_PROMPT));
	GetDlgItem(IDC_GOTODAY_DATE_ST)->SetWindowText(GET_RES_STRING(IDS_GOTODAY_DATE_ST));
	GetDlgItem(IDC_GOTODAY_SHOW_MODE_ST)->SetWindowText(GET_RES_STRING(IDS_GOTODAY_SHOW_MODE_ST));
	GetDlgItem(IDOK)->SetWindowText(GET_RES_STRING(IDS_APP_IDOK));
	GetDlgItem(IDCANCEL)->SetWindowText(GET_RES_STRING(IDS_APP_IDCANCEL));
	
	m_wndCombo.AddString(GET_RES_STRING(IDS_CALENDAR_DAY_PANEL));
	m_wndCombo.AddString(GET_RES_STRING(IDS_CALENDAR_WORK_WEEK_PANEL));
	m_wndCombo.AddString(GET_RES_STRING(IDS_CALENDAR_WEEK_PANEL));
	m_wndCombo.AddString(GET_RES_STRING(IDS_CALENDAR_MONTH_PANEL));
	m_wndCombo.SetCurSel(m_TypeIndex);
}

void 
CGotoDayDialog::DSkinLiteSkin()
{

	dsSkinWindow( GetSafeHwnd(), SKIN_TYPE_DIALOG, _T("gotoday_dlg_xml"), TRUE);
	
	dsSetDrawItemVisible(GetSafeHwnd(), 0, 0, FALSE, FALSE);
	dsSetDrawItemVisible(GetSafeHwnd(), 1, 1, TRUE, TRUE);
}

void 
CGotoDayDialog::add_anchor_ex()
{
	AddAnchor( GetDlgItem(IDC_GOTODAY_DATE_ST)->m_hWnd, TOP_LEFT, TOP_RIGHT );
	AddAnchor( GetDlgItem(IDC_DATETIMEPICKER)->m_hWnd, TOP_LEFT, TOP_RIGHT );
	AddAnchor( GetDlgItem(IDC_GOTODAY_SHOW_MODE_ST)->m_hWnd, TOP_LEFT, TOP_RIGHT );
	AddAnchor( GetDlgItem(IDC_MODE_COMBO)->m_hWnd, TOP_LEFT, TOP_RIGHT );
	AddAnchor( GetDlgItem(IDOK)->m_hWnd, BOTTOM_RIGHT, BOTTOM_RIGHT );
	AddAnchor( GetDlgItem(IDCANCEL)->m_hWnd, BOTTOM_RIGHT, BOTTOM_RIGHT );
	
}

void 
CGotoDayDialog::OnClose()
{
	CDialogBase::OnClose();
	g_dll_obj->erase_lock_wnd(this);
}
